﻿/*
--------------------------------------
File: SimpleColorDetect.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
A very simple color based localisation algorithm
--------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Costella;

namespace THO7_Team8
{
	internal class SimpleColorDetectMonogrome : VisionAlgorithm
	{
		public SimpleColorDetectMonogrome(String name)
			: base(name)
		{
			hasConfiguration = false;
		}

		public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
		{
			//Image for returning only the license plate without
			Bitmap returnImage = new Bitmap(sourceImage);

			//The original array
			int[] rgbValues = MyImage.BmpToArray(returnImage);

			//The modified array
			int size = rgbValues.Length;
			int[] rgbValues2 = new int[size];

			//Yellow color 2
			//int margin = 50;
			//int minblue = 0; int maxblue = 153;
			//int mingreen = 255 - 50; int maxgreen = 255;
			//int minred = 255 - 50; int maxred = 255;

			//Yellow color
			int margin = 100;
			int minblue = 17 - (margin / 3);
			int maxblue = 17 + (margin / 3);
			int mingreen = 204 - margin;
			int maxgreen = 204 + margin;
			int minred = 255 - margin;
			int maxred = 255 + margin;

			//Main loop which checks for yellow pixels
			for(int i = 0; i < size; i++)
			{
				int blue = rgbValues[i] & 255;
				int green = (rgbValues[i] >> 8) & 255;
				int red = (rgbValues[i] >> 16) & 255;

				//float hue = HSVConvertor.getHue(blue, green, red);
				//MessageBox.Show("" + hue);

				//if (hue >= 40 && hue <= 55)
				//{
				//If the current pixel is yellow
				if(blue <= maxblue &&
					blue >= minblue &&
					green <= maxgreen &&
					green >= mingreen &&
					red <= maxred &&
					red >= minred)
				{
					//If the current pixel is yellow, copy the color from the original image into the modified array
					rgbValues2[i] = WHITE;
				}
				else
				{
					//If the current pixel is not yellow, make it black in the modified image array
					rgbValues2[i] = BLACK;
				}
			}
			returnImage = MyImage.ArrayToBmp(rgbValues2, returnImage);
			return returnImage;
		}
	}
}